﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JinShengService;
using System.Collections.ObjectModel;
using System.Windows.Data;
using EHR.Common;

namespace CommunitySystem.PageView
{
    public partial class JSCJListControl : UserControl
    {
        private JinShengServiceClient client = new JinShengServiceClient();
        public JSCJListControl()
        {
            InitializeComponent();

            //处级的 人事干部只能增加不能删除
            if (Session.LoginUser.RoleID == 1)
                this.btnDele.Visibility = Visibility.Visible;
            else this.btnDele.Visibility = Visibility.Collapsed;

            IsWorking.Visibility = Visibility.Visible;
            busy.BusyContent = "正在同步数据，请稍后...";

            client.GetJYCompleted += new EventHandler<GetJYCompletedEventArgs>(client_GetJYCompleted);
            client.GetJYCountCompleted += new EventHandler<GetJYCountCompletedEventArgs>(client_GetJYCountCompleted);
       
            Query();
        }
        private int pageSize = 20;
        void client_GetJYCountCompleted(object sender, GetJYCountCompletedEventArgs e)
        {
            ObservableCollection<int> list = new ObservableCollection<int>();
            int last = e.Result % pageSize;
            int count = 0;
            if (last > 0)
                count = e.Result / pageSize + 1;
            else
                count = e.Result / pageSize;
            for (int i = 0; i < count; i++)
            {
                list.Add(i);
            }

            PagedCollectionView pcv = new PagedCollectionView(list);
            dataPager1.PageSize = 1;
            dataPager1.Source = null;
            dataPager1.Source = pcv;
        }

        void client_GetJYCompleted(object sender, GetJYCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
            IsWorking.Visibility = Visibility.Collapsed;
        }
       /// <summary>
       /// 新增
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            JSCJWindow win = new JSCJWindow();
            win.Show();
            win.Closed += new EventHandler(win_Closed);
        }

        void win_Closed(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
          
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = string.Format(" and emp.createuser='{0}'", Session.LoginUser.LoginName);
            client.GetJYCountAsync(tj);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDele_Click(object sender, RoutedEventArgs e)
        {
            var sel = this.dg.SelectedItem as Emp_CJJS;
            if (sel == null)
            { 
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的信息！");
                return;
            }
            if (MessageBox.Show("确定要删除该信息吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                client.JYDelCompleted -= new EventHandler<JYDelCompletedEventArgs>(client_JYDelCompleted);
                client.JYDelCompleted += new EventHandler<JYDelCompletedEventArgs>(client_JYDelCompleted);
                client.JYDelAsync(sel);
            }
        }
        void client_JYDelCompleted(object sender, JYDelCompletedEventArgs e)
        {
            if (e.Result)
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功！");

            else
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除失败！");

            Query();
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
        }
        private void dataPager1_PageIndexChanged(object sender, EventArgs e)
        {
            int index = dataPager1.PageIndex == -1 ? 0 : dataPager1.PageIndex;
            string tj = string.Empty;
            if (Session.LoginUser.RoleID != 1)
                tj = string.Format(" and emp.createuser='{0}'", Session.LoginUser.LoginName);
            client.GetJYAsync(tj, index + 1, pageSize);
        }
    }
}
